package com.wisely.ch8_5.service;

import com.wisely.ch8_5.domain.Person;
import com.wisely.ch8_5.repository.IPersonDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class PersonService implements IPersonService {

    @Autowired
    private IPersonDao personDao;

    @Override
    @CachePut(value = "people",key = "#person.id.toString()")
    //@CachePut(value = "people",keyGenerator = "wiselyKeyGenerator")
    public Person save(Person person) {
        Person p = personDao.insert(person);
        System.out.println("为id,key为:" + p.getId() + "数据生成了缓存");
        return p;
    }

    @Override
    @CacheEvict(value = "people",key = "#id.toString()")
    public void remove(Long id) {
        System.out.println("删除id,key为:" + id + "的缓存");
        personDao.delete(id);
    }

    @Override
    @Cacheable (value = "people", key = "#person.id.toString()")
    public Person findOne(Person person) {
        Person p = personDao.findOne(person.getId());
        System.out.println("为id,key为:" + p.getId() + "数据生成了缓存");
        return p;
    }

}
